package com.ruoyi.provider.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.provider.domain.AhctSurvey;
import com.ruoyi.provider.domain.AhctSurveyCode;
import com.ruoyi.provider.domain.ReviewResponse;
import com.ruoyi.provider.mapper.AhctImageMapper;
import com.ruoyi.provider.mapper.AhctSurveyMapper;
import com.ruoyi.provider.service.AhctImageService;
import com.ruoyi.provider.service.AhctSurveyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author SunNan
 * @version 1.0
 * @date 2024/5/15 14:27
 * @description
 */
@Service
public class AhctSurveyServiceImpl implements AhctSurveyService {


    @Resource
    private AhctSurveyMapper ahctSurveyMapper;

    @Resource
    private AhctImageMapper ahctImageMapper;

    @Resource
    private AhctImageService ahctImageService;



    //获取查勘管理列表
    @Override
    public PageInfo<AhctSurvey> selectSurveyList(AhctSurvey ahctSurvey, int pageNum, int pageSize) {
        // 开启分页
        PageHelper.startPage(pageNum,pageSize);
        // 执行查询
        List<AhctSurvey> surveyList = ahctSurveyMapper.selectSurveyList(ahctSurvey);
        // 封装为PageInfo
        PageInfo<AhctSurvey> pageInfo = new PageInfo<>(surveyList);
        return pageInfo;
    }


    //查勘管理信息录入
    @Override
    public void insertSurvey(AhctSurveyCode ahctSurveyCode) {


        List<String> list = ahctSurveyCode.getAddress_code();
        String addResCode = String.join(",",list);
        AhctSurvey ahctSurver = new AhctSurvey();
        ahctSurver.setAddress_code(addResCode);
        ahctSurver.setCar_number(ahctSurveyCode.getCar_number());
        ahctSurver.setArea(ahctSurveyCode.getArea());
        ahctSurver.setCity(ahctSurveyCode.getCity());
        ahctSurver.setAccident_address(ahctSurveyCode.getAccident_address());
        ahctSurver.setCompany_name(ahctSurveyCode.getCompany_name());
        ahctSurver.setProvince(ahctSurveyCode.getProvince());
        ahctSurver.setSurveyer(ahctSurveyCode.getSurveyer());
        ahctSurver.setCase_number(ahctSurveyCode.getCase_number());
        ahctSurver.setCase_type(ahctSurveyCode.getCase_type());
        ahctSurveyMapper.insertSurvey(ahctSurver);

        //新增查勘图片
        ahctImageService.insertImage(0,ahctSurveyCode.getCase_number(),ahctSurveyCode.getImgList());
    }


    //查勘管理信息修改
    @Transactional
    @Override
    public void updateSurvey(AhctSurveyCode ahctSurveyCode) {

        if (ahctSurveyCode.getImgList() == null){
            List<String> list = ahctSurveyCode.getAddress_code();
            String addResCode = String.join(",",list);
            AhctSurvey ahctSurver = new AhctSurvey();
            ahctSurver.setAddress_code(addResCode);
            ahctSurver.setId(ahctSurveyCode.getId());
            ahctSurver.setCar_number(ahctSurveyCode.getCar_number());
            ahctSurver.setArea(ahctSurveyCode.getArea());
            ahctSurver.setCity(ahctSurveyCode.getCity());
            ahctSurver.setAccident_address(ahctSurveyCode.getAccident_address());
            ahctSurver.setCompany_name(ahctSurveyCode.getCompany_name());
            ahctSurver.setProvince(ahctSurveyCode.getProvince());
            ahctSurver.setSurveyer(ahctSurveyCode.getSurveyer());
            ahctSurver.setCase_number(ahctSurveyCode.getCase_number());
            ahctSurver.setSun_company_name(ahctSurveyCode.getSun_company_name());
            ahctSurver.setCase_type(ahctSurveyCode.getCase_type());
            ahctSurver.setStatus(ahctSurveyCode.getStatus());
            ahctSurveyMapper.updateSurvey(ahctSurver);
        }else {

        //修改图片,先进行数据库图片删除,在新增图片到数据库
            ahctImageService.deleteImg(ahctSurveyCode.getCase_number());
            ahctImageService.insertImage(0,ahctSurveyCode.getCase_number(),ahctSurveyCode.getImgList());
        }

    }


    //查勘管理信息删除
    @Transactional
    @Override
    public void deleteSurvey(AhctSurveyCode ahctSurveyCode) {
        ahctSurveyMapper.deleteSurvey(ahctSurveyCode.getId());

        //查勘图片删除
        ahctImageMapper.deleteImg(ahctSurveyCode.getCase_number());

    }


    //修改查勘类型状态
    @Override
    public void updateSurveyType(Integer case_type, String case_number) {
        ahctSurveyMapper.updateSurveyType(case_type,case_number);
    }


    //查勘审核
    @Override
    public ReviewResponse updateReviewApproval(AhctSurvey ahctSurvey) {

        ReviewResponse response = new ReviewResponse();

        int statusAll = 3;

        if (ahctSurvey.getStatus() != statusAll) { // 审核未通过的处理
            response.setMessage("Review rejected.");//Review rejected.审核拒绝
            response.setReason(ahctSurvey.getDescribe());
        } else {
            // 审核通过的处理
            ahctSurveyMapper.updateSurveyState(ahctSurvey);
            response.setMessage("Review approved.");//Review approved.审核批准
        }
        return response;
    }

    @Override
    public String selectCaseNumber(String case_number) {
        String caseNumber;
        caseNumber = ahctSurveyMapper.selectCaseNumber(case_number);
        return caseNumber;
    }
}
